package com.zqw.action;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zqw.info.AdminInfo;
import com.zqw.info.ResponseMsg;
import com.zqw.info.RoleInfo;
import com.zqw.pojo.AdminPojo;
import com.zqw.pojo.AuthPojo;
import com.zqw.pojo.RolePojo;
import com.zqw.service.AdminService;
import com.zqw.service.RoleService;
import com.zqw.util.MD5Util;
import com.zqw.util.MyStaticFinal;
import com.zqw.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
public class AdminController {

    @Autowired
    AdminService adminService;

    @Autowired
    RoleService roleService;

    @RequestMapping("sole")
    @ResponseBody
    public ResponseMsg sole(AdminInfo adminInfo,RoleInfo roleInfo){
        boolean b = false;
        if(adminInfo!=null){
            b = adminService.soleAdmin(adminInfo);
        }else if(roleInfo!=null){
            b = roleService.soleRole(roleInfo);
        }
        ResponseMsg responseMsg = new ResponseMsg();
        if(b){
            responseMsg.setResult(MyStaticFinal.SUCCESS_RESULT_ADMIN);
        }else{
            responseMsg.setResult(MyStaticFinal.ERROR_RESULT_ADMIN);
        }
        return responseMsg;
    }

    //管理员登录
    @RequestMapping("adminLogin")
    @ResponseBody
    public String adminLogin(AdminInfo adminInfo, HttpServletRequest req){
        int i = 1;
        if(StringUtil.isEmpty(adminInfo.getAccount())||StringUtil.isEmpty(adminInfo.getAccount())){
        }else{
            adminInfo.setApass(MD5Util.getMd5(adminInfo.getApass()));
            AdminPojo adminPojo = adminService.adminLogin(adminInfo);
            if(adminPojo != null){
                req.getSession().setAttribute("admin",adminPojo);
                List<AuthPojo> list = new ArrayList<AuthPojo>();
                List<RolePojo> roles = adminPojo.getRoles();
                Set<AuthPojo> authlist = new HashSet<AuthPojo>();
                for (int j = 0; j < roles.size(); j++) {
                    authlist.addAll(roles.get(j).getAuths());
                }
                String auths = "";
                for (AuthPojo auth : authlist) {
                    if(auth.getAutype().equals("1")){
                        auth.setAupath(auth.getAupath().replace("/",""));
                        list.add(auth);
                    }
                    auths+="/"+auth.getAupath();
                }
                Collections.sort(list,new Comparator<AuthPojo>(){
                    public int compare(AuthPojo o1, AuthPojo o2) {
                        if(o1.getAuid() > o2.getAuid()){
                            return 1;
                        }
                        if(o1.getAuid() == o2.getAuid()){
                            return 0;
                        }
                        return -1;
                    }
                });
                req.getSession().setAttribute("pagelist",list);
                req.getSession().setAttribute("auths",auths);
                i=0;
            }
        }
        return "{\"msg\":\""+i+"\"}";
    }

    //转发主页
    @RequestMapping("index")
    public String index(HttpServletRequest request){
        return "index";
    }


    //转发到登录界面
    @RequestMapping("jump")
    public String jump(HttpServletRequest request){
        request.getSession().removeAttribute("admin");
        request.getSession().removeAttribute("auths");
        request.getSession().removeAttribute("pagelist");
        return "login";
    }

    //根据条件查询管理员列表
    @RequestMapping("adminList")
    public String adminList(HttpServletRequest req ,AdminInfo adminInfo){
        PageHelper.startPage(adminInfo.getPageNum(),adminInfo.getPageSize());
        List<AdminPojo> adminPojos = adminService.adminList(adminInfo);
        PageInfo<AdminPojo> list = new PageInfo<>(adminPojos);
        req.setAttribute("aname",adminInfo.getAname());
        req.setAttribute("account",adminInfo.getAccount());
        req.setAttribute("pageInfo",list);
        return "admin-list";
    }

    //根据id删除管理员
    @RequestMapping("delAdmin")
    @ResponseBody
    public ResponseMsg delAdmin(String id){
        boolean b = adminService.delOneAdmin(id);
        ResponseMsg responseMsg = new ResponseMsg();
        if(b){
            responseMsg.setResult(MyStaticFinal.SUCCESS_RESULT_ADMIN);
        }else{
            responseMsg.setResult(MyStaticFinal.ERROR_RESULT_ADMIN);
            responseMsg.setResultMsg("删除失败");
        }
        return responseMsg;
    }

    //根据一组id删除管理员
    @RequestMapping("delAdmins")
    @ResponseBody
    public ResponseMsg delAdmins(String ids){
        ResponseMsg responseMsg = new ResponseMsg();
        if(StringUtil.isEmpty(ids)) {
            responseMsg.setResult(MyStaticFinal.ERROR_RESULT_ADMIN);
            responseMsg.setResultMsg("删除失败");
        }else{
            boolean b = adminService.delAdmins(ids);
            if (b) {
                responseMsg.setResult(MyStaticFinal.SUCCESS_RESULT_ADMIN);
            } else {
                responseMsg.setResult(MyStaticFinal.ERROR_RESULT_ADMIN);
                responseMsg.setResultMsg("删除失败");
            }
        }
        return responseMsg;
    }

    //转发到添加管理员界面
    @RequestMapping("adminAdd")
    public String adminAdd(HttpServletRequest req){
        List<RolePojo> rolePojos = roleService.queryAllRole(new RoleInfo());
        req.setAttribute("roles",rolePojos);
        return "admin-add";
    }

    //添加管理员
    @RequestMapping("addAdmin")
    @ResponseBody
    public ResponseMsg addAdmin(AdminInfo adminInfo){
        ResponseMsg responseMsg = new ResponseMsg();
        adminInfo.setApass(MD5Util.getMd5(adminInfo.getApass()));
        boolean b = adminService.addAdmin(adminInfo);
        if(b){
            responseMsg.setResult(MyStaticFinal.SUCCESS_RESULT_ADMIN);
        }else{
            responseMsg.setResult(MyStaticFinal.ERROR_RESULT_ADMIN);
            responseMsg.setResultMsg("添加失败");
        }
        return responseMsg;
    }

    //转发到修改管理员界面
    @RequestMapping("adminEdit")
    public String adminEdit(HttpServletRequest req,int aid){
        List<RolePojo> rolePojos = roleService.queryAllRole(new RoleInfo());
        req.setAttribute("roles",rolePojos);
        AdminInfo adminInfo = new AdminInfo();
        adminInfo.setAid(aid);
        AdminPojo adminPojo = adminService.adminLogin(adminInfo);
        req.setAttribute("adminPojo",adminPojo);
        return "admin-edit";
    }

    //修改管理员
    @RequestMapping("updAdmin")
    @ResponseBody
    public ResponseMsg updAdmin( AdminInfo adminInfo){
        ResponseMsg responseMsg = new ResponseMsg();
        boolean b = false;
        try {
            b = adminService.updAdmin(adminInfo);
        } catch (Exception e) {
            e.printStackTrace();
            b = false;
        }finally {
            if(b){
                responseMsg.setResult(MyStaticFinal.SUCCESS_RESULT_ADMIN);
            }else{
                responseMsg.setResult(MyStaticFinal.ERROR_RESULT_ADMIN);
                responseMsg.setResultMsg("修改失败");
            }
            return responseMsg;
        }
    }

    //修改用户状态
    @RequestMapping("updAdminStatus")
    @ResponseBody
    public ResponseMsg updAdminStatus(AdminInfo adminInfo){
        ResponseMsg responseMsg = new ResponseMsg();
        boolean b = adminService.updAdminStatus(adminInfo);
        if(b){
            responseMsg.setResult(MyStaticFinal.SUCCESS_RESULT_ADMIN);
        }else{
            responseMsg.setResult(MyStaticFinal.ERROR_RESULT_ADMIN);
            responseMsg.setResultMsg("修改失败");
        }
        return responseMsg;
    }

    //跳转修改密码
    @RequestMapping("passwd")
    public String passwd(){
        return "passwd";
    }

    //修改密码
    @RequestMapping("passEdit")
    @ResponseBody
    public ResponseMsg passEdit(String pass,HttpServletRequest request){
        ResponseMsg responseMsg = new ResponseMsg();
        if(StringUtil.isEmpty(pass)){
            responseMsg.setResult(MyStaticFinal.ERROR_RESULT_ADMIN);
            responseMsg.setResultMsg("修改失败");
            return responseMsg;
        }
        pass = MD5Util.getMd5(pass);
        boolean b = false;
        try {
            Integer admin = ((AdminPojo) request.getSession().getAttribute("admin")).getAid();
            AdminInfo adminInfo = new AdminInfo();
            adminInfo.setAid(admin);
            adminInfo.setApass(pass);
            b = adminService.updAdmin(adminInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(b){
                responseMsg.setResult(MyStaticFinal.SUCCESS_RESULT_ADMIN);
            }else{
                responseMsg.setResult(MyStaticFinal.ERROR_RESULT_ADMIN);
                responseMsg.setResultMsg("修改失败");
            }
            return responseMsg;
        }
    }

}
